import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:ichinesereader/models/assessment/assessmentQueryDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonsTeacherSwitchClass.dart';
import 'package:ichinesereader/widgets/grids/assessmentsDataGrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';

class AssessmentsManagement extends StatefulWidget {
  static const String routeName = '/teacher/assessments/management';

  @override
  State<AssessmentsManagement> createState() => _AssessmentsManagementState();
}

class _AssessmentsManagementState extends State<AssessmentsManagement> {
  final box = Hive.box('appconfig');
  final AssessmentsController controller = Get.find();

  @override
  void initState() {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    String _classId = box.get(CacheManagerKey['currentclass'].toString());

    // ClassDetailsNew _classDetailsNew = ClassDetailsNew();
    // try {
    //   _classDetailsNew =
    //       classDetailsNewFromJson(box.get('currentclassdetails'));
    // } catch (error) {}
    controller.assessItemsForTeacher.clear();

    if (isTeacher) {
      AssessmentQueryDto dto = new AssessmentQueryDto();
      dto.live = true;
      dto.queued = true;
      dto.past = false;

      Future.delayed(Duration(milliseconds: 100), () {
        controller.fetchAssessmentsByClass(_classId, dto);
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build AssessmentsManagement');

    return Scaffold(
      backgroundColor: Colors.transparent,
      floatingActionButton: FloatingActionButtonsTeacherSwitchClass(),
      body: controller.obx(
        (data) =>
            AssessmentsDataGrids(true, controller.assessItemsForTeacher, true),
        onEmpty:
            AssessmentsDataGrids(true, controller.assessItemsForTeacher, true),
        onLoading: Container(),
      ),
    );
  }
}
